package com.ruoyi.web.shopping.controller;


import com.ruoyi.common.core.domain.R;
import com.ruoyi.web.shopping.service.CaptchasService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/captcha")
@Slf4j
@Api(tags = "验证码相关接口")
public class CaptchasController {

    @Autowired
    private CaptchasService captchaService;

//    @GetMapping("/get/{id}")
//    public R<String> getTemeplate(@PathVariable String id) {
//        log.info("查询模板id为{}", id);
//        Captcha captcha = captchaService.getById(id);
//        return R.ok(captcha.getTemplate());
//    }


    /**
     * 随机获取模板
     *
     * @return
     */
    @GetMapping("/get")
    @ApiOperation("随机获取模板")
    public R<String> getAllTemeplate() {
        String captchaTem = captchaService.getAllTemeplate();
        return R.ok(captchaTem);
    }

    /**
     * 根据用户id、模板id以及答案进行验证码登录
     *
     * @param userid
     * @param templateid
     * @param result
     * @return
     */
    @GetMapping("/get/{userid}/{templateid}/{result}")
    @ApiOperation("验证码登录")
    public R<String> getAllTemeplate(@PathVariable String userid, @PathVariable String templateid, @PathVariable String result) {
        String captchaTem = captchaService.selectByTemplateId(userid, templateid, result);
        return R.ok(captchaTem);
    }

    /**
     * 根据用户id判断验证码是否过期
     *
     * @param userid
     * @return
     */
    @GetMapping("/getuser/{userid}")
    @ApiOperation("判断验证码是否过期")
    public R<String> getUserId(@PathVariable String userid) {
        String user = captchaService.getUserid(userid);
        return R.ok(user);
    }
}
